﻿using System.Collections.Generic;
using FileSecurityManager.BizEntities;

namespace FileSecurityManager.BizLogic
{
    using System.IO;

    using FileSecurityManager.DataAccess.Repositories;

    public class UsersLogic : BaseLogic
    {
        /// <summary>
        /// Gets all users.
        /// </summary>
        /// <returns>The list of all users in the system</returns>
        public List<UserModel> GetAllUsers()
        {
            using (var repository = new UserLinq2SqlRepository())
            {
                return repository.GetAll();
            }
        }

        /// <summary>
        /// Adds the user.
        /// </summary>
        /// <param name="user">The new user model.</param>
        public void AddUser(UserModel user)
        {
            using (var repository = new UserLinq2SqlRepository())
            {
                repository.Create(user);
            }
        }

        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="user">The user model.</param>
        public void UpdateUser(UserModel user)
        {
            using (var repository = new UserLinq2SqlRepository())
            {
                repository.Update(user);
            }

            if (user.UserOptions != null && 
                string.IsNullOrEmpty(user.UserOptions.WorkingDir) == false)
            {
                if (user.UserOptions.IsExcel)
                {
                    createDirectory(Path.Combine(user.UserOptions.WorkingDir, "Excel Docs"));
                }

                if (user.UserOptions.IsImages)
                {
                    createDirectory(Path.Combine(user.UserOptions.WorkingDir, "Images"));
                }

                if (user.UserOptions.IsMusic)
                {
                    createDirectory(Path.Combine(user.UserOptions.WorkingDir, "Music Files"));
                }

                if (user.UserOptions.IsPDF)
                {
                    createDirectory(Path.Combine(user.UserOptions.WorkingDir, "PDF Docs"));
                }

                if (user.UserOptions.IsVideo)
                {
                    createDirectory(Path.Combine(user.UserOptions.WorkingDir, "Video Files"));
                }

                if (user.UserOptions.IsWord)
                {
                    createDirectory(Path.Combine(user.UserOptions.WorkingDir, "Word Docs"));
                }
            }
        }

        /// <summary>
        /// Deletes the user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        public void DeleteUser(int userId)
        {
            using (var repository = new UserLinq2SqlRepository())
            {
                repository.Delete(userId);
            }
        }

        /// <summary>
        /// Creates the directory.
        /// </summary>
        /// <param name="dirPath">The dir path.</param>
        private void createDirectory(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }
        }
    }
}
